import {
    LOGIN_HOST,
    API_HOST
} from '../config';
let MALL_HOST = 'https://hmthm.joymew.com/';
import {
    getToken,
    clearStorage
} from './storage.js';
import {
    toDefaultPage
} from './navigate'

const checkTargetUrl = function (domain, url) {
    let targetUrl = `${domain}${url}`
    if (url.substr(0, 1) === '/') {
        
        url = url.substr(1, url.length)
    }
    if (domain[domain.length - 1] !== '/') {
        
        targetUrl = `${domain}/${url}`
    }
    return targetUrl
}
class Http {
    constructor() {}
    request(url, method = 'GET', data, loadingText = '数据加载中...') {
        return new Promise((resolve, reject) => {
            if (loadingText) {
                wx.showLoading({
                    title: loadingText,
                    mask: true,
                });
            }
            const token = getToken();
            wx.request({
                url: checkTargetUrl(LOGIN_HOST, url),
                method,
                timeout: 10000,
                header: {
                    token,
                    'Content-Type': 'application/x-www-form-urlencoded'
                },
                data,
                success: res => {

                    if (loadingText) {
                        wx.hideLoading()
                    }

                    if (res.statusCode === 200) {
                        if (res.data.success) {
                            if (res.data.message == '401') {
                                wx.showToast({
                                    title: '登录已过期,请重新登录!',
                                    icon: 'none'
                                });
                                clearStorage();
                                getApp().globalData.token = ''
                                toDefaultPage();
                                reject('401');
                            } else {
                                resolve(res.data.data)
                            }
                        } else {
                            if (res.data.message == '401') {
                                wx.showToast({
                                    title: '登录已过期,请重新登录!',
                                    icon: 'none'
                                });
                                clearStorage();
                                getApp().globalData.token = ''
                                toDefaultPage()
                                reject('401');
                            }
                            reject('网络异常')
                        }
                    } else {
                        reject('网络异常')
                    }
                },
                fail: err => {

                    if (loadingText) {
                        wx.hideLoading()
                    }
                    reject('网络异常')
                }

            })

        })
    }

    post(url, data, loadingText = '数据加载中...') {
        return this.request(url, 'POST', data, loadingText = '数据加载中...')
    }

    get(url, data, loadingText = '数据加载中...') {
        return this.request(url, 'GET', data, loadingText = '数据加载中...')
    }
    $request(url, method = 'GET', data, loadingText = '数据加载中...') {
        return new Promise((resolve, reject) => {
            if (loadingText && loadingText !== 'cancelLoading') {
                wx.showLoading({
                    title: loadingText,
                    mask: true,
                });
            }
            const token = getToken();
            wx.request({
                url: checkTargetUrl(API_HOST, url),
                method,
                timeout: 10000,
                header: {
                    token,
                    'Content-Type': 'application/x-www-form-urlencoded'
                },
                data,
                success: res => {

                    if (loadingText) {
                        wx.hideLoading()
                    }

                    if (res.statusCode === 200) {
                        if (res.data) {
                            if (res.data.message == '401') {
                                wx.showToast({
                                    title: '登录已过期,请重新登录!',
                                    icon: 'none'
                                });
                                clearStorage();
                                getApp().globalData.token = ''
                                toDefaultPage()
                                reject('401');
                            } else {
                                resolve(res.data)
                            }

                        } else {
                            reject('网络异常')
                        }
                    } else {
                        reject('网络异常')
                    }
                },
                fail: err => {

                    if (loadingText) {
                        wx.hideLoading()
                    }
                    reject('网络异常')
                }

            })

        })
    }

    $post(url, data, loadingText = '数据加载中...') {
        return this.$request(url, 'POST', data, loadingText)
    }

    $get(url, data, loadingText = '数据加载中...') {
        return this.$request(url, 'GET', data, loadingText = '数据加载中...')
    }
    $$request(url, method = 'GET', data, loadingText = '数据加载中...') {
      return new Promise((resolve, reject) => {
          if (loadingText && loadingText !== 'cancelLoading') {
              wx.showLoading({
                  title: loadingText,
                  mask: true,
              });
          }
          const token = getToken();
          wx.request({
              url: checkTargetUrl(MALL_HOST, url),
              method,
              timeout: 10000,
              header: {
                  token,
                  'Content-Type': 'application/x-www-form-urlencoded'
              },
              data,
              success: res => {

                  if (loadingText) {
                      wx.hideLoading()
                  }

                  if (res.statusCode === 200) {
                      if (res.data) {
                          if (res.data.message == '401') {
                              wx.showToast({
                                  title: '登录已过期,请重新登录!',
                                  icon: 'none'
                              });
                              clearStorage();
                              getApp().globalData.token = ''
                              toDefaultPage()
                              reject('401');
                          } else {
                              resolve(res.data)
                          }

                      } else {
                          reject('网络异常')
                      }
                  } else {
                      reject('网络异常')
                  }
              },
              fail: err => {
                  console.log(err)
                  if (loadingText) {
                      wx.hideLoading()
                  }
                  reject('网络异常')
              }

          })

      })
  }

}

export default new Http()